﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using forum.ApplicationCode;
using forum.Data;

namespace forum
{
    public partial class Thread : System.Web.UI.Page
    {

        int ThreadId
        {
            get
            {
                if (Request.QueryString["ThreadId"] != null)
                {
                    int threadId;
                    if (int.TryParse(Request.QueryString["ThreadId"], out threadId))
                    {
                        return threadId;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            // Reload the page, not from broswer's cache
            Response.AppendHeader("Cache-Control", "no-store");
            //clean thread from unread thread session
            if (Session["unreadThreads"] != null)
            {
                removeThreadFromUnreadSession();

            }

            if (Membership.GetUserName() == "")
                lnkbtnAddPost.Visible = false;
            hdnfldThreadId.Value = ThreadId.ToString();

            using (forumDataContext data = new forumDataContext())
            {
                var threadDetalis = data.sp_getThreadFullDetails(ThreadId);
                if (threadDetalis==null)
                    Response.Redirect("forumWelcome.asp");
                var thread = threadDetalis.FirstOrDefault();
                lblSubForumeName.Text = "Sub forum is: "+thread.subForumTopic;

                lblUserName.Text = thread.userName;
                lblCreationDate.Text = thread.creationDate.ToString();
                lblThreadTitile.Text = thread.threadTitle;
                lblContent.Text = thread.threadContent;

                lvPosts.DataSource = data.sp_GetThreadPostList(ThreadId);
                lvPosts.DataBind();
            }


        }

        private void removeThreadFromUnreadSession()
        {
            List<int> tmp = Session["unreadThreads"] as List<int>;
            if(tmp.Contains(ThreadId))
                tmp.Remove(ThreadId);
            Session["unreadThreads"] = tmp;
        }

        protected void lvPosts_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            using (forumDataContext data = new forumDataContext())
            {
                int postId = ((sp_GetThreadPostListResult)DataBinder.GetDataItem(e.Item)).postId;
                int ownerId=((sp_GetThreadPostListResult)DataBinder.GetDataItem(e.Item)).ownerId;
   
                Label uName = e.Item.FindControl("lblUserName") as Label;
                uName.Text = data.sp_GetUserNameByUserId(ownerId).FirstOrDefault().userName;
                HyperLink linkEdit = e.Item.FindControl("hyplnkEdit") as HyperLink;
                HyperLink linkdel = e.Item.FindControl("lbkbtnDelete") as HyperLink;

                if (!isAllowEdit(postId))
                {
                    linkEdit.Visible = false;
                    linkdel.Visible = false;

                }

            }

        }


        protected Boolean isAllowEdit(int postId)
        {
            using (forumDataContext data = new forumDataContext())
            {

                //only an owner allow to edit a thread

                Boolean isOwner = false;

                if (data.sp_IsOwnerOfPost(Membership.GetUserId(), postId).FirstOrDefault().isOwner > 0)
                        isOwner = true;
                return (isOwner);


                /*
                Boolean isAdmin = Membership.IsAdmin();

                int subForumId = data.sp_getThreadFullDetails(ThreadId).FirstOrDefault().subForumId;
                Boolean isModerator = Membership.IsModerator(subForumId);
                Boolean isOwner = false;

                if (data.sp_IsOwnerOfPost(Membership.GetUserId(), postId).FirstOrDefault().isOwner > 0)
                        isOwner = true;
                return (isAdmin || isModerator || isOwner);
                 */

            }

            
        }

    }
}